package com.example.demo.controller;

import com.example.demo.domain.Result;
import com.example.demo.repository.GirlRepository;
import com.example.demo.servers.GirlServers;
import com.example.demo.domain.Girl;
import com.example.demo.untils.ResultUntil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
public class GirlsController {
    @Autowired
    private GirlRepository girlRepository;
    @Autowired
    private GirlServers girlServers;
    /**
     * 查询所有女生列表
     * @return
     */
    private final static Logger logger= LoggerFactory.getLogger(GirlsController.class);
    @GetMapping(value = "/girls")
    public List<Girl> girlList(){
          logger.info("girlList");
        return girlRepository.findAll();
    }

    /**
     * 添加一个女生

     * @return
     */
    @PostMapping(value = "/girls")

    public Result<Girl> addGirl(@Valid Girl girl, BindingResult bindingResult){

        if(bindingResult.hasErrors()){
            /**
             * 简化代码（失败）
             */
//              Result  result=new Result();
//              result.setCode(0);
//              result.setMsg(bindingResult.getFieldError().getDefaultMessage());
//              result.setData(null);
//              return  result;
              //return ResultUntil.error(0,bindingResult.getFieldError().getDefaultMessage());
            return null;

        }
        girl.setCupSize(girl.getCupSize());
        girl.setAge(girl.getAge());
        /**
         * 简化代码（成功）
         */
//        Result result=new Result();
//        result.setCode(1);
//        result.setMsg("成功传入");
//        result.setData(girlRepository.save(girl));
//
//       return result;
            return  ResultUntil.success(girlRepository.save(girl));
    }
    /**
     * 查询一个女生
     */
    @GetMapping(value = "/girls/{id}")
     public  Girl FindoneGirl(@PathVariable("id") Integer id){

               return girlRepository.findOne(id);

    }

    /**
     * 更新一个女生信息
     */
    public Girl updateGirl(@PathVariable("id") Integer id,
                           @RequestParam("cupSize") String cupSize,
                           @RequestParam("age") Integer age){

        Girl girl =new Girl();
        girl.setId(id);
        girl.setCupSize(cupSize);
        girl.setAge(age);

        return    girlRepository.save(girl);


    }
    @PutMapping(value="/girls/{id}")
    /**
     * 删除一个女生信息
     */

   @DeleteMapping(value = "/girls/{id}")
    public void DeleteGirl(@PathVariable("id") Integer id){
        girlRepository.delete(id);
   }

   //通过年龄来查询女生列表
   @GetMapping(value = "/girls/age/{age}")
public  List<Girl> girlListByAge(@PathVariable("age") Integer age){
        return girlRepository.findByAge(age);
    }
    //增加两个女生
    @PostMapping(value = "/girls/two")
    public void  addTwoGirls(){
        girlServers.insertTwo();
    }
    //获取年龄并进行判断
    @GetMapping(value = "/girls/getAge/{id}")
    public void getAge(@PathVariable("id") Integer id) throws  Exception{
        girlServers.getAge(id);
    }
}
